package com.lagou.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;


@Activate(group = {CommonConstants.CONSUMER})
public class TransportIPFilter implements Filter {

    public final ThreadLocal<String> IPHolder = new ThreadLocal<>();

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        RpcContext.getContext().setAttachment("ip", IPHolder.get());
        return invoker.invoke(invocation);
    }

    public void setIPHolder(String ip) {
        IPHolder.remove();
        IPHolder.set(ip);
    }
}
